package com.qiaotongtianxia.loans.net;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.HttpHeaders;

/**
 * lisen
 */

public class LogForJson {
    /**
     * 将json字符串格式化
     *
     * @param json
     * @return
     */
    public static String warp(String json) {
        if (TextUtils.isEmpty(json)) {
            return "json数据是空";
        }
        try {
            JSONObject jsonObject = new JSONObject(json);
            return jsonObject.toString(1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

    public static String logForResponse(Response response) {
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        ResponseBody responseBody = clone.body();
        try {
            if (HttpHeaders.hasBody(clone)) {
                return warp(responseBody.string());
            }

        } catch (Exception e) {
        }
        return "";
    }

}
